package com.hamilton.erik.pt.mobile.util;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

public class ServerUtil {
	
	public static final String ENDPOINT_PROTOCOL = "https://";
	public static final String ENDPOINT_URL = "ec2-54-84-99-2.compute-1.amazonaws.com";
	public static final String ENDPOINT_PORT = "8443";

	public static final String AUTHENTICATION_PATH = "auth";
	public static final String ANIMAL_PATH = "animal";
	public static final String ENCLOSURE_PATH = "enclosure";
	public static final String ENCLOSURE_MAINTENANCE_PATH = "enclosure/maintenance";
	public static final String FEEDING_PATH = "feeding";
	public static final String MEDICAL_RECORD_PATH = "medicalRecord";
	
	private HostnameVerifier customHostnameVerifier = null; 
	
	private static ServerUtil instance = null;
	
	public static ServerUtil getInstance() {
		if(instance == null) {
			instance = new ServerUtil();
		}
		
		return instance;
	}

	private ServerUtil() {}

	public final String buildEndpoint() {
		StringBuilder endpoint = new StringBuilder();
		
		endpoint.append(ENDPOINT_PROTOCOL)
				.append(ENDPOINT_URL)
				.append(":")
				.append(ENDPOINT_PORT);
		
		return endpoint.toString();
	}
	
	public final String buildAuthenticationPath() {
		StringBuilder url = new StringBuilder(buildEndpoint());
		
		url.append("/").append(AUTHENTICATION_PATH);
		
		return url.toString();
	}
	
	public final String buildAnimalPath() {
		StringBuilder url = new StringBuilder(buildEndpoint());
		
		url.append("/").append(ANIMAL_PATH);
		
		return url.toString(); 
	}
	
	public final String buildEnclosurePath() {
		StringBuilder url = new StringBuilder(buildEndpoint());
		
		url.append("/").append(ENCLOSURE_PATH);
		
		return url.toString(); 
	}
	
	public final String buildEnclosureMaintenancePath() {
		StringBuilder url = new StringBuilder(buildEndpoint());
		
		url.append("/").append(ENCLOSURE_MAINTENANCE_PATH);
		
		return url.toString(); 
	}
	
	public final String buildFeedingPath() {
		StringBuilder url = new StringBuilder(buildEndpoint());
		
		url.append("/").append(FEEDING_PATH);
		
		return url.toString(); 
	}
	
	public final String buildMedicalRecordPath() {
		StringBuilder url = new StringBuilder(buildEndpoint());
		
		url.append("/").append(MEDICAL_RECORD_PATH);
		
		return url.toString(); 
	}
	
	public HostnameVerifier getPtHostnameVerifier() {
		if(customHostnameVerifier == null) {
			customHostnameVerifier =
				new HostnameVerifier() {
					@Override
					public boolean verify(String hostname, SSLSession session) {
						if(hostname.equals(ENDPOINT_URL)) {
							return true;
						}
						
						return false;
					}
				};
		}
		
		return customHostnameVerifier;
	}
}
